/*
 * Sokoban.java
 *
 * Created on 29 de diciembre de 2006, 11:28
 */
package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author zur
 */




public class Sokoban extends MIDlet implements CommandListener {
    
    /** Creates a new instance of Sokoban */
    public Sokoban() {
        canvas = new SCanvas(this);
    }
    
    private Command exitCommand;//GEN-BEGIN:MVDFields
    private org.netbeans.microedition.lcdui.SplashScreen Logozur;
    private Image image1;
    private Font font1;
    private List Menu;
    private List Niveles;
    private TextBox Ayuda;
    private Command exitCommand1;
    private Command exitCommand2;
    private Command exitCommand3;
    private Command okCommand1;
    private Command okCommand2;
    private Command exitCommand4;
    private Ticker ticker1;//GEN-END:MVDFields
    /** canvas*/
    public SCanvas canvas;
    /**  display*/
    private Display display;
    private Command restartCommand;
    private Command undoCommand;
//GEN-LINE:MVDMethods
    
    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize() {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        getDisplay().setCurrent(get_Logozur());//GEN-LINE:MVDInitInit
        // Insert post-init code here
    }//GEN-LINE:MVDInitEnd
    
    /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin
        
        // Insert global pre-action code here
        if (displayable == Logozur) {//GEN-BEGIN:MVDCABody
            if (command == Logozur.DISMISS_COMMAND) {//GEN-END:MVDCABody
                
                getDisplay().setCurrent(get_Menu());//GEN-LINE:MVDCAAction7
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase7
        } else if (displayable == Menu) {
            if (command == Menu.SELECT_COMMAND) {
                switch (get_Menu().getSelectedIndex()) {
                    case 0://GEN-END:MVDCACase7
                        // Insert pre-action code here
                        canvas.startGame(0);
                        new Thread(canvas).start();  //ojo
                        getDisplay().setCurrent(canvas);
                        canvas.addCommand(get_restartCommand());
                        canvas.addCommand(get_undoCommand());
                        canvas.setCommandListener(this);
                        // Do nothing//GEN-LINE:MVDCAAction23
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase23
                    case 1://GEN-END:MVDCACase23
                        // Insert pre-action code here
                        getDisplay().setCurrent(get_Niveles());//GEN-LINE:MVDCAAction25
                        Listar(canvas.loadNivel());
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase25
                    case 2://GEN-END:MVDCACase25
                        // Insert pre-action code here
                        getDisplay().setCurrent(get_Ayuda());//GEN-LINE:MVDCAAction27
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase27
                }
            } else if (command == exitCommand4) {//GEN-END:MVDCACase27
                // Insert pre-action code here
                exitMIDlet();//GEN-LINE:MVDCAAction46
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase46
        } else if (displayable == Niveles) {
            if (command == exitCommand2) {//GEN-END:MVDCACase46
                // Insert pre-action code here
                getDisplay().setCurrent(get_Menu());//GEN-LINE:MVDCAAction36
                // Insert post-action code here
            } else if (command == okCommand2) {//GEN-LINE:MVDCACase36
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction44
                int nivel = get_Niveles().getSelectedIndex();
                if(nivel!=-1){
                    canvas.startGame(nivel+1);
                    new Thread(canvas).start();
                    canvas.addCommand(get_restartCommand());
                    canvas.addCommand(get_undoCommand());
                    canvas.setCommandListener(this);
                    getDisplay().setCurrent(canvas);
                }
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase44
        } else if (displayable == Ayuda) {
            if (command == exitCommand3) {//GEN-END:MVDCACase44
                // Insert pre-action code here
                getDisplay().setCurrent(get_Menu());//GEN-LINE:MVDCAAction38
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase38
        }//GEN-END:MVDCACase38
        else if (displayable == canvas){
            if (command == restartCommand) {  
                String level = canvas.getCurrentLevel();
                canvas.startGame(Integer.parseInt(level));
            } else if (command == undoCommand) {
                canvas.undoSoko();
                canvas.undobox();
            }
        }
        // Insert global post-action code here
}//GEN-LINE:MVDCAEnd
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay() {//GEN-FIRST:MVDGetDisplay
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet
    
    
    
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin5
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {//GEN-END:MVDGetBegin5
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit5
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd5
        return exitCommand;
    }//GEN-END:MVDGetEnd5
    
    /** This method returns instance for Logozur component and should be called instead of accessing Logozur field directly.//GEN-BEGIN:MVDGetBegin6
     * @return Instance for Logozur component
     */
    public org.netbeans.microedition.lcdui.SplashScreen get_Logozur() {
        if (Logozur == null) {//GEN-END:MVDGetBegin6
            // Insert pre-init code here
            Logozur = new org.netbeans.microedition.lcdui.SplashScreen(getDisplay());//GEN-BEGIN:MVDGetInit6
            Logozur.setCommandListener(this);
            Logozur.setFullScreenMode(true);
            Logozur.setText("");
            Logozur.setImage(get_image1());
            Logozur.setTextFont(get_font1());
            Logozur.setTimeout(1000);//GEN-END:MVDGetInit6
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd6
        return Logozur;
    }//GEN-END:MVDGetEnd6
    
    /** This method returns instance for image1 component and should be called instead of accessing image1 field directly.//GEN-BEGIN:MVDGetBegin8
     * @return Instance for image1 component
     */
    public Image get_image1() {
        if (image1 == null) {//GEN-END:MVDGetBegin8
            // Insert pre-init code here
            try {//GEN-BEGIN:MVDGetInit8
                image1 = Image.createImage("/hello/zurlogo.jpg");
            } catch (java.io.IOException exception) {
                exception.printStackTrace();
            }//GEN-END:MVDGetInit8
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd8
        return image1;
    }//GEN-END:MVDGetEnd8
    
    /** This method returns instance for font1 component and should be called instead of accessing font1 field directly.//GEN-BEGIN:MVDGetBegin13
     * @return Instance for font1 component
     */
    public Font get_font1() {
        if (font1 == null) {//GEN-END:MVDGetBegin13
            // Insert pre-init code here
            font1 = Font.getFont(Font.FACE_PROPORTIONAL, 0x1, Font.SIZE_LARGE);//GEN-LINE:MVDGetInit13
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd13
        return font1;
    }//GEN-END:MVDGetEnd13
    
    /** This method returns instance for Menu component and should be called instead of accessing Menu field directly.//GEN-BEGIN:MVDGetBegin20
     * @return Instance for Menu component
     */
    public List get_Menu() {
        if (Menu == null) {//GEN-END:MVDGetBegin20
            // Insert pre-init code here
            Menu = new List(null, Choice.IMPLICIT, new String[] {//GEN-BEGIN:MVDGetInit20
                "Continuar",
                "Nivel",
                "Ayuda"
            }, new Image[] {
                null,
                null,
                null
            });
            Menu.addCommand(get_exitCommand4());
            Menu.setCommandListener(this);
            Menu.setSelectedFlags(new boolean[] {
                true,
                false,
                false
            });//GEN-END:MVDGetInit20
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd20
        return Menu;
    }//GEN-END:MVDGetEnd20
    
    /** This method returns instance for Niveles component and should be called instead of accessing Niveles field directly.//GEN-BEGIN:MVDGetBegin30
     * @return Instance for Niveles component
     */
    public List get_Niveles() {
        if (Niveles == null) {//GEN-END:MVDGetBegin30
            // Insert pre-init code here
            Niveles = new List(null, Choice.IMPLICIT, new String[0], new Image[0]);//GEN-BEGIN:MVDGetInit30
            Niveles.addCommand(get_exitCommand2());
            Niveles.addCommand(get_okCommand2());
            Niveles.setCommandListener(this);
            Niveles.setTicker(get_ticker1());
            Niveles.setSelectedFlags(new boolean[0]);//GEN-END:MVDGetInit30
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd30
        return Niveles;
    }//GEN-END:MVDGetEnd30
    
    /** This method returns instance for Ayuda component and should be called instead of accessing Ayuda field directly.//GEN-BEGIN:MVDGetBegin32
     * @return Instance for Ayuda component
     */
    public TextBox get_Ayuda() {
        if (Ayuda == null) {//GEN-END:MVDGetBegin32
            // Insert pre-init code here
            Ayuda = new TextBox(null, "Sokoban\nAutor: zur - innovacion movil\n           tel - 3216899\n           www.parquesoftpereira.com/zur\n\nPresione la teclas 2, 8, 4, y 6 para dirigir al Sokoban\n\nPresione la tecla 5 para volver al menu principal.\n\nEl Sokoban empujara solo una caja a la vez.", 1000, TextField.ANY | TextField.UNEDITABLE);//GEN-BEGIN:MVDGetInit32
            Ayuda.addCommand(get_exitCommand3());
            Ayuda.setCommandListener(this);//GEN-END:MVDGetInit32
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd32
        return Ayuda;
    }//GEN-END:MVDGetEnd32
    
    /** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.//GEN-BEGIN:MVDGetBegin33
     * @return Instance for exitCommand1 component
     */
    public Command get_exitCommand1() {
        if (exitCommand1 == null) {//GEN-END:MVDGetBegin33
            // Insert pre-init code here
            exitCommand1 = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit33
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd33
        return exitCommand1;
    }//GEN-END:MVDGetEnd33
    
    /** This method returns instance for exitCommand2 component and should be called instead of accessing exitCommand2 field directly.//GEN-BEGIN:MVDGetBegin35
     * @return Instance for exitCommand2 component
     */
    public Command get_exitCommand2() {
        if (exitCommand2 == null) {//GEN-END:MVDGetBegin35
            // Insert pre-init code here
            exitCommand2 = new Command("Atras", Command.EXIT, 1);//GEN-LINE:MVDGetInit35
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd35
        return exitCommand2;
    }//GEN-END:MVDGetEnd35
    
    /** This method returns instance for exitCommand3 component and should be called instead of accessing exitCommand3 field directly.//GEN-BEGIN:MVDGetBegin37
     * @return Instance for exitCommand3 component
     */
    public Command get_exitCommand3() {
        if (exitCommand3 == null) {//GEN-END:MVDGetBegin37
            // Insert pre-init code here
            exitCommand3 = new Command("Atras", Command.EXIT, 1);//GEN-LINE:MVDGetInit37
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd37
        return exitCommand3;
    }//GEN-END:MVDGetEnd37
    
    /** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly.//GEN-BEGIN:MVDGetBegin41
     * @return Instance for okCommand1 component
     */
    public Command get_okCommand1() {
        if (okCommand1 == null) {//GEN-END:MVDGetBegin41
            // Insert pre-init code here
            okCommand1 = new Command("Jugar", Command.OK, 1);//GEN-LINE:MVDGetInit41
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd41
        return okCommand1;
    }//GEN-END:MVDGetEnd41
    
    /** This method returns instance for okCommand2 component and should be called instead of accessing okCommand2 field directly.//GEN-BEGIN:MVDGetBegin43
     * @return Instance for okCommand2 component
     */
    public Command get_okCommand2() {
        if (okCommand2 == null) {//GEN-END:MVDGetBegin43
            // Insert pre-init code here
            okCommand2 = new Command("Jugar", Command.OK, 1);//GEN-LINE:MVDGetInit43
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd43
        return okCommand2;
    }//GEN-END:MVDGetEnd43
    
    /** This method returns instance for exitCommand4 component and should be called instead of accessing exitCommand4 field directly.//GEN-BEGIN:MVDGetBegin45
     * @return Instance for exitCommand4 component
     */
    public Command get_exitCommand4() {
        if (exitCommand4 == null) {//GEN-END:MVDGetBegin45
            // Insert pre-init code here
            exitCommand4 = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit45
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd45
        return exitCommand4;
    }//GEN-END:MVDGetEnd45

    /** This method returns instance for ticker1 component and should be called instead of accessing ticker1 field directly.//GEN-BEGIN:MVDGetBegin47
     * @return Instance for ticker1 component
     */
    public Ticker get_ticker1() {
        if (ticker1 == null) {//GEN-END:MVDGetBegin47
            // Insert pre-init code here
            ticker1 = new Ticker("Nivel   Puntaje");//GEN-LINE:MVDGetInit47
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd47
        return ticker1;
    }//GEN-END:MVDGetEnd47
    public Command get_restartCommand(){
        if (restartCommand == null) {
            restartCommand = new Command("Reiniciar", Command.EXIT, 1);
        }
        return restartCommand;
    }
    public Command get_undoCommand(){
        if (undoCommand == null) {
            undoCommand = new Command("Deshacer", Command.BACK, 1);
        }
        return undoCommand;
    }
    
    public void startApp() {
        
        initialize();
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    private void Listar(String [] Niveles) {
        get_Niveles().deleteAll();
        for(int i=Niveles.length-1; i>0;i-=2) {
            get_Niveles().append(Niveles[i-1]+"     "+Niveles[i],null);
        }
    }
    
    void menuMostar() {
        getDisplay().setCurrent(get_Menu());
    }
}





